package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.result.Result;
import com.example.entity.yifei.Change;
import com.example.entity.yifei.Copte;
import com.example.entity.yifei.Purte;
import com.example.mapper.ChangeMapper;
import com.example.mapper.CopteMapper;
import com.example.mapper.PurteMapper;
import com.example.service.CopteService;
import com.example.untils.MapUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mgkj
 * @since 2024-08-01
 */
@Api(tags = "字段变更")
@RestController
@RequestMapping("/api/change")
public class ChangeController {

    @Resource
    private ChangeMapper changeMapper;

    @Resource
    private CopteService copteService;

    @Resource
    private PurteMapper purteMapper;

    @Resource
    private CopteMapper copteMapper;

    @ApiOperation("查询")
    @GetMapping("getAll")
    public Result getAll(Change change){
        IPage<Change> page = new Page<>(change.getPage(),change.getPageSize());
        LambdaQueryWrapper<Change> wrapper = new LambdaQueryWrapper<>();
        if (change.getCreator()!= null && !change.getCreator().isEmpty()){
            wrapper.like(Change::getCreator,"%"+change.getCreator()+"%");
        }
        if (change.getCreateTime()!= null && !change.getCreateTime().isEmpty()){
            wrapper.like(Change::getCreateTime,"%"+change.getCreateTime()+"%");
        }
        if (change.getModifyTime()!= null && !change.getModifyTime().isEmpty()){
            wrapper.like(Change::getModifyTime,"%"+change.getModifyTime()+"%");
        }
        if (change.getTableFrom()!= null && !change.getTableFrom().isEmpty()){
            wrapper.like(Change::getTableFrom,"%"+change.getTableFrom()+"%");
        }
        if (change.getField()!= null && !change.getField().isEmpty()){
            wrapper.like(Change::getField,"%"+change.getField()+"%");
        }
        IPage<Change> changes = changeMapper.selectPage(page,wrapper);
        Map<String, Object> mapMp = MapUtils.getMapMp(changes);
        return Result.success(mapMp);
    }

    @ApiOperation("新增")
    @PutMapping("insert")
    public Result insert(@RequestBody Change change) {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyyMMdd");
        if (change.getCreateTime()==null||change.getCreateTime().isEmpty()){
            change.setCreateTime(dateTime.format(date));
        }
        int insert = changeMapper.insert(change);
        if (insert > 0) {
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }

    @ApiOperation("删除")
    @DeleteMapping("delete/{id}")
    public Result insert(@PathVariable Integer id) {
        int insert = changeMapper.delete(new QueryWrapper<Change>().eq("id",id));
        if (insert > 0) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation("根据id查询")
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable Integer id) {
        Change change = changeMapper.selectById(id);
        return Result.success(change);
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public Result update(@RequestBody Change change) {
        QueryWrapper<Change> wrapper = new QueryWrapper<>();
        wrapper.eq("id",change.getId());
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyyMMdd");
        if (change.getModifyTime() == null || change.getModifyTime().isEmpty()){
            change.setModifyTime(dateTime.format(date));
        }
        int update = changeMapper.update(change, wrapper);
        if (update > 0) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @ApiOperation("采购变更单")
    @GetMapping("purChangeList")
    public Result purChangeList(@RequestParam(defaultValue = "1") Long current,
                                @RequestParam(defaultValue = "10") Long size){
        IPage<Purte> page = new Page<>(current,size);
        IPage<Purte> purteIPage = purteMapper.selectPage(page,new QueryWrapper<Purte>().eq("TE017", "Y"));
        Map<String, Object> mapMp = MapUtils.getMapMp(purteIPage);
        return Result.success(mapMp);
    }

    @ApiOperation("销售变更单")
    @PostMapping("copChangeList")
    public Result<PageInfo<List<Copte>>> copChangeList(@RequestParam(defaultValue = "1") int current,
                                                       @RequestParam(defaultValue = "10") int size,
                                                       @RequestBody Copte dto){
        PageHelper.startPage(current, size, "COPTE.CREATE_DATE DESC");
        List<Copte> list = copteMapper.selectListDto(dto);

        PageInfo<List<Copte>> pageInfo = new PageInfo(list);
        return Result.success(pageInfo);
    }

//    @ApiOperation("销售变更插入")
//    @PostMapping("insertCopChange")
//    public Result insertCopChange(@RequestBody CopChangeDto changeDto){
//        Result result = copteService.insertCopChange(changeDto);
//        return result.success();
//    }
}

